# @FileName  :  course.py
# @Time      :  2025/7/29
# @Author    :  Ayanami
# @info      :  课程相关的Pydantic模型

from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
from app.models.models import DictionaryType
from app.schemas.student import DictionaryResponse


class CourseBase(BaseModel):
    """课程基础模型"""
    title: str = Field(..., max_length=100, description="课程名称")
    parent_course: Optional[int] = Field(None, description="父课程ID")
    category_id: int = Field(..., description="课程类型ID")
    is_online: bool = Field(True, description="是否在线课程")
    cover_image: Optional[str] = Field(None, max_length=255, description="封面图片URL")
    video_resource: Optional[str] = Field(None, max_length=255, description="视频云资源ID")
    live_url: Optional[str] = Field(None, max_length=255, description="直播地址")
    schedule: Optional[datetime] = Field(None, description="直播/上课时间")
    is_premium: bool = Field(False, description="是否精品课")
    enable_checkin: bool = Field(False, description="是否启用签到")
    notes: Optional[str] = Field(None, description="备注")
    tags: List[str] = Field(default_factory=list, description="课程标签")
    agent_id: int = Field(..., description="培训机构ID")
    teacher_id: int = Field(..., description="讲师ID")


class CourseCreate(CourseBase):
    """创建课程模型"""
    pass


class CourseUpdate(BaseModel):
    """更新课程模型"""
    title: Optional[str] = Field(None, max_length=100, description="课程名称")
    parent_course: Optional[int] = Field(None, description="父课程ID")
    category_id: Optional[int] = Field(None, description="课程类型(字典)")
    is_online: Optional[bool] = Field(None, description="是否在线课程")
    cover_image: Optional[str] = Field(None, max_length=255, description="封面图片URL")
    video_resource: Optional[str] = Field(None, max_length=255, description="视频云资源ID")
    live_url: Optional[str] = Field(None, max_length=255, description="直播地址")
    schedule: Optional[datetime] = Field(None, description="直播/上课时间")
    is_premium: Optional[bool] = Field(None, description="是否精品课")
    enable_checkin: Optional[bool] = Field(None, description="是否启用签到")
    notes: Optional[str] = Field(None, description="备注")
    tags: Optional[List[str]] = Field(default=None, description="课程标签")
    agent_id: Optional[int] = Field(None, description="培训机构ID")
    teacher_id: Optional[int] = Field(None, description="讲师ID")


class TeacherInfo(BaseModel):
    """教师信息"""
    id: int
    name: str
    teacher_id: str
    specialty: str

    class Config:
        from_attributes = True


class AgentInfo(BaseModel):
    """机构信息"""
    id: int
    name: str
    service_direction: str

    class Config:
        from_attributes = True


class CourseInDB(CourseBase):
    """数据库中的课程模型"""
    id: int
    enrollment_count: int = Field(default=0, description="报名人数")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class CourseResponse(CourseInDB):
    """课程响应模型"""
    teacher: Optional[TeacherInfo] = None
    agent: Optional[AgentInfo] = None
    category: Optional[DictionaryResponse] = None


class CourseListResponse(BaseModel):
    """课程列表响应模型"""
    total: int
    items: List[CourseResponse]


class CourseQuery(BaseModel):
    """课程查询参数"""
    title: Optional[str] = Field(None, description="课程名称模糊查询")
    category_id: Optional[int] = Field(None, description="课程类型")
    is_online: Optional[bool] = Field(None, description="是否在线课程")
    is_premium: Optional[bool] = Field(None, description="是否精品课")
    enable_checkin: Optional[bool] = Field(None, description="是否启用签到")
    teacher_id: Optional[int] = Field(None, description="讲师ID")
    agent_id: Optional[int] = Field(None, description="培训机构ID")
    parent_course: Optional[int] = Field(None, description="父课程ID")
    tags: Optional[List[str]] = Field(None, description="标签筛选")
    schedule_start: Optional[datetime] = Field(None, description="上课时间开始")
    schedule_end: Optional[datetime] = Field(None, description="上课时间结束")
    skip: int = Field(0, ge=0, description="跳过记录数")
    limit: int = Field(10, ge=1, le=1000, description="返回记录数")


class CourseStatistics(BaseModel):
    """课程统计信息"""
    total_courses: int = Field(description="总课程数")
    online_courses: int = Field(description="在线课程数")
    offline_courses: int = Field(description="线下课程数")
    premium_courses: int = Field(description="精品课程数")
    total_enrollments: int = Field(description="总报名人数")
    avg_enrollment: float = Field(description="平均报名人数")


class SubCourseResponse(BaseModel):
    """子课程响应模型"""
    id: int
    title: str
    is_online: bool
    schedule: Optional[datetime]
    enrollment_count: int
    created_at: datetime

    class Config:
        from_attributes = True